package com.yjy.service;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjy.entity.Account;
import com.yjy.entity.Admin;
import com.yjy.entity.User;
import com.yjy.exception.CustomerException;
import com.yjy.mapper.UserMapper;
import com.yjy.utils.TokenUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Resource
    UserMapper userMapper;

    public String user(String name) {
        if("user".equals(name)){
            return "user";
        }else{
//            return "账号错误";//自定义异常
            throw new CustomerException("账号错误!");
        }
    }

    public List<User> selectAll(User user){
        return userMapper.selectAll(user);
    }

    public PageInfo<User> selectPage(Integer pageNum, Integer pageSize,User user) {
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userMapper.selectAll(user);
        return PageInfo.of(users);
    }

    public void add(User user) {
        // 根据新的账号查询数据库 是否存在同样账号的数据
        User dbUser = userMapper.selectByUsername(user.getUsername());
        if(dbUser!=null){
            throw new CustomerException("账号重复");

        }
//        if(user.getPassword() == null || "".equals(user.getPassword())){
//
//        }
        //用hutool
        //默认密码
        if(StrUtil.isBlank(user.getPassword())) {
            user.setPassword("user");
        }

        if(StrUtil.isBlank(user.getName())) {
            user.setName(user.getUsername());
        }
        user.setRole("USER");
        userMapper.insert(user);
    }

    public void update(User user) {
        userMapper.updateById(user);
    }

    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    public void deleteBatch(List<User> list) {
//        for (User user : list) { //iter快捷键
//            deleteById(user.getId());
//        }  效率太低

        userMapper.deleteBatch(list);
    }

    public User login(Account user) {
        User dbUser = userMapper.selectByUsername(user.getUsername());
        if(dbUser == null){
            throw new CustomerException("账号不存在");
        }
        // 验证账号密码是否正确
        if(!dbUser.getPassword().equals(user.getPassword())){
            throw  new CustomerException("账号或密码错误");
        }
        //创建token并返回给前端
        String token = TokenUtils.createToken(dbUser.getId() + "-" + "USER", dbUser.getPassword());
        dbUser.setToken(token);
        return dbUser;
    }

    public void register(User user) {
        this.add(user);
    }

    public Account selectById(String userId) {
        return userMapper.selectById(userId);
    }

    public void updatePassword(Account account) {
        // 先判断新密码和确认密码是否一致
        if(!account.getNewPassword().equals(account.getNew2Password())) {
            throw new CustomerException("500","您两次输入的密码不一致!请重新输入!");
        }
        // 校验一下原密码是否正确
        Account currentUser = TokenUtils.getCurrentUser();
        if(!account.getPassword().equals(currentUser.getPassword())){
            throw new CustomerException("500","原密码输入错误!");
        }

        //开始更新密码
        User user = userMapper.selectById(String.valueOf(currentUser.getId()));
        user.setPassword(account.getNewPassword());
        userMapper.updateById(user);
    }
}






















